﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MenesDroid.EngineCharacter;

namespace XNA_Game_Engine.GUI
{
    public class TimeAndStart
    {
        public bool GameStarted { private set; get; }
        public bool Executed { set; get; }
        public double StartedTime { private set; get; }
        public double ActionStartedTime { private set; get; }
        public double TimeToActivate { private set; get; }
        public double RunningTime { private set; get; }
        public string DisplayedTime { private set; get; }

        public int LastActionActivatedTime { private set; get; }
        public int ActionIndex { set; get; }
        public int InsideFunctionActionIndex { set; get; }

        private bool _mustGetTime;
        private bool _mustGetTimeNow;

        public GameState State { set; get; }

        public enum GameState
        {
            Started,
            Stopped,
            Finished
        }

        public TimeAndStart()
        {
            State = GameState.Stopped;
            GameStarted = false;
            _mustGetTime = true;
            StartedTime = 0;
            ActionStartedTime = 0;
            TimeToActivate = 0;
            ActionIndex = 0;
            InsideFunctionActionIndex = 0;
            LastActionActivatedTime = 0;
            DisplayedTime = "00:00:00";
            Executed = true;
        }

        public void Update(GameTime gameTime)
        {
            switch (State)
            {
                case GameState.Stopped:
                    {
                        if (_mustGetTime)
                        {
                            StartedTime = gameTime.TotalGameTime.TotalMilliseconds;
                            _mustGetTime = false;
                        }

                        RunningTime += gameTime.ElapsedGameTime.Milliseconds;

                        double run = RunningTime/1000;

                        var minutes = (int) (run/60);
                        var seconds = (int) run%60;
                        var millis = (int) (((run - (minutes*60)) - seconds)*100);

                        DisplayedTime =
                            (minutes < 10 ? "0" + minutes : minutes.ToString()) + ":" +
                            (seconds < 10 ? "0" + seconds : seconds.ToString()) + ":" +
                            (millis < 10 ? "0" + millis : millis.ToString());
                        break;
                    }
                case GameState.Started:
                    {
                        if (_mustGetTimeNow)
                        {
                            ActionStartedTime = gameTime.TotalGameTime.TotalMilliseconds;
                            _mustGetTimeNow = false;
                        }

                        TimeToActivate += gameTime.ElapsedGameTime.Milliseconds;

                        if (TimeToActivate < 1000)
                            return;

                        TimeToActivate = 0;

                        for (int i = ActionIndex; i < StaticGui.PanelForCommands.Slots.Count; i++)
                        {
                            ActionIndex = i;
                            if (StaticGui.PanelForCommands.Slots[i].Actions == null)
                                continue;

                            if (StaticGui.SelectedSlot != null)
                                StaticGui.SelectedSlot.IsSelected = false;

                            StaticGui.SelectedSlot = StaticGui.PanelForCommands.Slots[i];
                            StaticGui.SelectedSlot.IsSelected = true;
                            StaticGui.PanelForCommands.Slots[i].Actions();

                            if (!Executed)
                            {
                                Executed = true;
                                continue;
                            }

                            break;
                        }

                        ActionIndex++;

                        if (ActionIndex != StaticGui.PanelForCommands.Slots.Count)
                            return;

                        Stop();
                        break;
                    }
            }
        }

        public void Draw(SpriteBatch render)
        {
            ShowTime(render);
        }

        public void Start()
        {
            State = GameState.Started;
            GameStarted = true;
            _mustGetTimeNow = true;
            StaticGui.PlayButton.SetEnabled(false);
            StaticGui.StopButton.SetEnabled(true);
        }

        public void Stop()
        {
            State = GameState.Stopped;
            GameStarted = false;
            _mustGetTime = false;
            ActionIndex = 0;
            LastActionActivatedTime = 0;
            TimeToActivate = 0;
            StaticGui.TimeStart.InsideFunctionActionIndex = 0;
            StaticGui.PlayButton.SetEnabled(true);
            StaticGui.StopButton.SetEnabled(false);
            ((Robot) StaticGui.PlayerReference).jumpFalse();
            StaticGui.SelectedSlot.IsSelected = false;
            StaticGui.SelectedSlot = null;
        }

        public void FinishGame()
        {
            State = GameState.Finished;
        }

        public void ShowTime(SpriteBatch render)
        {
            render.DrawString(StaticGui.Font, DisplayedTime, new Vector2(400, 0), Color.White);
        }

        public bool gameStarted
        {
            get { return GameStarted; } //coco}
             
        }

        public double runningTime
        {
            get {return RunningTime; }
        }

    }
}
